package net.adetech.crj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.bean.WgrtbForm;
import net.adetech.crj.bean.ZggmtbForm;
import net.adetech.crj.bean.ZggmtbResult;
import net.adetech.crj.dao.ApplyMapper;
import net.adetech.crj.entity.Apply;
import net.adetech.crj.entity.User;
import net.adetech.crj.utils.ServiceUtil;
import net.adetech.framework.entity.ServiceRequest;
import net.adetech.framework.entity.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ApplyServiceController {

    @Autowired
    ServiceUtil serviceUtil;

    @RequestMapping(value = "/zggmtb/check", produces = "application/json")
    public String zggmtbCheck(ZggmtbForm form, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        ServiceResponse response = serviceUtil.callService("zggmtb.check", "user", user, "form", form);
        return JSON.toJSONString(response.getResults());
    }


    @RequestMapping(value = "/zggmtb/submit", produces = "application/json")
    public String zggmtbSubmit(ZggmtbForm form, HttpSession session) {

        User user = (User) session.getAttribute("currentUser");
        System.out.println(user.getIdNumber());


        ServiceRequest request = new ServiceRequest();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("form", form);
        params.put("user", user);
        request.setParams(params);
        ServiceResponse response = serviceUtil.call("zggmtb.submit", request);

        JSONObject result = JSONObject.parseObject(JSON.toJSONString(response.getResults().get("result")));


        JSONArray tipsArray = (JSONArray) response.get("tipsArray");
        String tipsString = "";
        if (tipsArray != null && tipsArray.size() > 0) {
            System.out.println(tipsArray.toJSONString());
            for (int i = 0; i < tipsArray.size(); i++) {
                JSONObject tips = tipsArray.getJSONObject(i);
                String reason = tips.getString("reason");
                if ("official".equals(reason)) {
                    JSONObject content = tips.getJSONObject("content");
                    String rszgdw = content.getJSONArray("data").getJSONObject(0).getString("rszgdw");
                    tipsString += ("系统提示:您是国家工作人员。");
                } else if ("age".equals(reason)) {
                    tipsString += ("系统提示:您未满16周岁。");
                } else if ("business".equals(reason)) {
                    JSONObject content = tips.getJSONObject("content");
                    int success = content.getInteger("success");
                    if (success == 1) {
                        continue;
                    } else {
                        tipsString += ("系统提示:您未进行商务备案。");

                    }
                }

                if (i < tipsArray.size() - 1) {
                    tipsString += "___";
                }

            }

        }
        result.put("tips", tipsString);
        return result.toJSONString();
    }


    @RequestMapping(value = "/wgrtb/submit", produces = "application/json")
    public String submit(WgrtbForm form) {

        ServiceRequest request = new ServiceRequest();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("form", form);
        request.setParams(params);
        ServiceResponse response = serviceUtil.call("wgrtb.submit", request);
        System.out.println("args_wgr---->" + response.get("args") + "\n");
        System.out.println("response_wgr---->" + response.get("response") + "\n");
        ZggmtbResult result = (ZggmtbResult) response.getResults().get("result");
        return JSON.toJSONString(response.getResults());
    }


    @RequestMapping(value = "/apply/list", produces = "application/json")
    public String getApplyList(HttpSession session) {
        User user = (User) session.getAttribute("currentUser");

        ServiceRequest request = new ServiceRequest();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("applyUserId", user.getIdNumber());
        params.put("user",user);
        request.setParams(params);
        ServiceResponse response = serviceUtil.call("apply.getApplyListByApplyUserId", request);
        List<Apply> applyList = (List<Apply>) response.get("applyList");


        return JSON.toJSONString(applyList);
    }

    @RequestMapping(value = "/apply/getById", produces = "application/json")
    public String getApplyById(HttpSession session, String applyId) {
        User user = (User) session.getAttribute("currentUser");
        Apply result = null;
        if (applyId != null) {
            ServiceResponse response = serviceUtil.callService("apply.getApplyById", "applyId", applyId);
            Apply apply = (Apply) response.getResults().get("apply");
            if (apply.getApplyUserId().equals(user.getIdNumber())) {
                result = apply;
            }
        }
        return result.getApplyDetail();
    }


}
